const {firefox, chromium} = require('playwright');

async function getCanUsePage(browser, url) {
  let page = await browser.newPage({
    proxy: {
      server: 'http://u5329.b5.t.16yun.cn:6460',
      username: '16GADNHJ',
      password: '936441'
    }
  });

  try {
    await page.goto(url, {waitUntil: 'domcontentloaded', timeout: 10000});
  } catch (e) {
    try {
      await page.goto(url, {waitUntil: 'domcontentloaded', timeout: 10000});
    } catch (e) {
      try {
        await page.goto(url, {waitUntil: 'domcontentloaded', timeout: 10000});
      } catch (e) {
        try {
          await page.goto(url, {waitUntil: 'domcontentloaded', timeout: 10000});
        } catch (e) {
          try {
            await page.goto(url, {waitUntil: 'domcontentloaded', timeout: 10000});
          } catch (e) {
            await page.goto(url, {waitUntil: 'domcontentloaded', timeout: 10000});
          }
        }
      }
    }
  }

  let now_url = await page.url();
  while (now_url == 'http://cli.tophub.today/tool/ip.unlock.php') {
    await page.waitForTimeout(1000);
    await page.close();
    page = await browser.newPage({
      proxy: {
        server: 'http://u5329.b5.t.16yun.cn:6460',
        username: '16GADNHJ',
        password: '936441'
      }
    });
    await page.goto(url, {waitUntil: 'domcontentloaded'});
    now_url = await page.url();
  }
  return page;
}

(async () => {
  console.time('chromium');
  let totalTopics = [];
  let page;
  let tab;
  // let url = 'https://tophub.today/n/KqndgxeLl9';
  // let url = 'https://tophub.today/n/Jb0vmloB1G';
  // let url = 'https://tophub.today/n/mproPpoq6O';
  // let url = 'https://tophub.today/n/5VaobgvAj1';
  let url = 'https://tophub.today/n/74KvxwokxM';

  const browser = await chromium.launch({
    headless: true,
  });

  page = await getCanUsePage(browser, url);

  await page.waitForSelector('div:nth-child(1) > div > div > div > table > tbody > tr');
  const eles = await page.$$('div:nth-child(1) > div > div > div > table > tbody > tr');

  let len = eles.length;

  for (let i = 0; i < eles.length; i++) {
    if (i > 0 && i % 10 == 0) {
      await page.close();
      page = await getCanUsePage(browser, url);
    }
    console.log('循环开始', i, len);
    let topic = {};
    let idx = `#page > div > div > div:nth-child(1) > div > div > div > table > tbody > tr:nth-child(${i + 1}) > td.al > a`;

    topic.title = await page.$eval(idx, el => el.innerText);
    await page.click(`${idx}`);
    await page.waitForTimeout(1000);
    let browserContext = browser.contexts()[0];

    let cnt = 0;
    let tabs = browserContext.pages();

    while (tabs.length < 2 && cnt < 5) {
      await page.waitForTimeout(1000);
      tabs = browserContext.pages();
      cnt++;
    }

    if (tabs.length != 2) {
      console.log('本次获取失败:', topic.title, i, len);
      continue;
    }

    tab = tabs[1];
    let original_url = tab.url();

    topic.content = original_url;

    if (tab) {
      await tab.close();
    }

    console.log('本次获取成功:', topic, i, len);
    totalTopics.push(topic);
  }

  console.log(totalTopics);
  console.log(url, 'success...');

})();
